package com.ebelter.ty.adapters;


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.ebelter.btcomlib.models.bluetooth.products.hrscale.HrScaleConstant;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.ty.R;
import com.ebelter.ty.adapters.bean.BluetoothDeviceInfo;

import java.util.ArrayList;
import java.util.List;

public class BluetoothSerarchAdapter extends BaseAdapter {

    private Context mContext;
    private List<BluetoothDeviceInfo> mDate;
    private BluetoothDeviceInfo bondBluetoothDeviceInfo;

    public List<BluetoothDeviceInfo> getmDate() {
        return mDate;
    }

    public void setBond(BluetoothDeviceInfo bond){
        this.bondBluetoothDeviceInfo = bond;
        notifyDataSetChanged();
    }


    public BluetoothSerarchAdapter(Context context, List<BluetoothDeviceInfo> mDate){
        this.mContext=context;
        this.mDate=mDate;
    }

    public void setmDate(List<BluetoothDeviceInfo> mDate) {
        this.mDate = mDate;
        notifyDataSetChanged();
    }



    public void addDate(BluetoothDeviceInfo item) {
        if (item!=null){
            if (mDate==null){
                mDate=new ArrayList<>();
            }
            mDate.add(item);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDate==null?0:mDate.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Vh vh;
        if (convertView==null){
            convertView=View.inflate(mContext, R.layout.item_searchblue_ly,null);
            vh=new Vh();
            vh.title=convertView.findViewById(R.id.item_searchblue_title);
            vh.address=convertView.findViewById(R.id.item_searchblue_address);
            vh.iv=convertView.findViewById(R.id.item_searchblue_iv);
            convertView.setTag(vh);
        }else {
            vh= (Vh) convertView.getTag();
        }

        BluetoothDeviceInfo bluetoothDevice=mDate.get(position);

        String name=bluetoothDevice.getName();
        if (name.contains(HrScaleConstant.HR_SCALE_NAME)){
            name= StringUtils.getResStr(R.string.scalename_name);
        }
        vh.title.setText(name);
        vh.address.setText(bluetoothDevice.getAddress());

        if (bondBluetoothDeviceInfo==null){
            vh.iv.setVisibility(View.INVISIBLE);
        }else {
            vh.iv.setVisibility(TextUtils.equals(bondBluetoothDeviceInfo.getAddress(),bluetoothDevice.getAddress())?View.VISIBLE:View.INVISIBLE);
        }


        return convertView;
    }


    private static class Vh{
        TextView title;
        TextView address;
        ImageView iv;
    }
}
